package com.qd.panda.shop.order;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.cdqidi.util.page.PageUtil;
import com.qd.common.panda.domain.entity.order.OrderStatisticsDTO;
import com.qd.common.sys.annotation.OperatorInfo;
import com.qd.common.sys.enums.OperatorType;
import com.qd.common.panda.domain.entity.shop.order.ProductOrderDTO;
import com.qd.panda.service.shop.order.ProductOrderService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.validation.constraints.NotBlank;

/**
 * WEB_商城订单
 *
 * @author sjk
 */
@RequiredArgsConstructor
@Validated
@RestController
@RequestMapping("/sys/productOrder/")
@Slf4j
public class ProductOrderController {
    private final ProductOrderService productOrderService;

    /**
     * 订单统计
     *
     * @param dto 查询参数
     * @return 统计信息
     */
    @PreAuthorize("@ps.hasPermission('system:productOrder:totalOrderStatistics')")
    @GetMapping(value = "totalOrderStatistics")
    public OrderStatisticsDTO totalOrderStatistics(ProductOrderDTO dto) {
        return productOrderService.totalOrderStatistics(dto);
    }

    /**
     * 订单列表
     *
     * @param dto 查询参数以及页数和每页显示条数
     * @return 列表信息
     */
    @PreAuthorize("@ps.hasPermission('system:productOrder:page')")
    @GetMapping(value = "/page")
    public IPage<ProductOrderDTO> page(ProductOrderDTO dto) {
        return productOrderService.page(PageUtil.buildPage(dto), dto);
    }

    /**
     * 获取订单
     *
     * @param id 主键
     * @return 记录
     */
    @GetMapping(value = "/getById")
    public ProductOrderDTO getById(@Validated @NotBlank(message = "ID不能为空") String id) {
        return productOrderService.getByIdDto(id);
    }

    /**
     * 订单导出
     *
     * @param query 查询参数
     */
    @PreAuthorize("@ps.hasPermission('system:productOrder:export')")
    @OperatorInfo(title = "导出", operator = OperatorType.EXPORT)
    @GetMapping(value = "/export")
    public void export(ProductOrderDTO query) {
        productOrderService.export(query);
    }
}